package com.blockmeta.bbs.businesslibrary.widget.chartview.Layer;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.DashPathEffect;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.Path;
import android.graphics.PathEffect;
import android.graphics.Point;
import android.graphics.RectF;

import com.blankj.utilcode.util.TimeUtils;
import com.blockmeta.bbs.baselibrary.utils.DimentionsUtil;
import com.blockmeta.bbs.baselibrary.utils.FormatUtil;
import com.blockmeta.bbs.businesslibrary.arouter.RouterHelper;
import com.blockmeta.bbs.businesslibrary.arouter.RouterPath;
import com.blockmeta.bbs.businesslibrary.arouter.interfaces.IDetailKlineBaseFragmentProvider;
import com.blockmeta.bbs.businesslibrary.widget.chartview.Axis.Coordinate;
import com.blockmeta.bbs.businesslibrary.widget.chartview.CandleData;
import com.blockmeta.bbs.businesslibrary.widget.chartview.ChartData;
import com.blockmeta.bbs.businesslibrary.widget.chartview.CoinChart;
import com.blockmeta.bbs.businesslibrary.widget.tictactec.ta.lib.Core;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;


abstract class BasicLayer
{
	protected LayerTypeCallback m_callback = null;
	protected boolean m_bGradient = false;

	private String m_strXText = null;
	private String m_strYText = null;
	private Point m_point = null;
	private CandleData mCandleData=null;

	protected float m_fYesterClose = Float.MIN_VALUE;
	protected boolean m_bShowYesterClose = false;

	private SimpleDateFormat m_Format = new SimpleDateFormat("yyyy/MM/dd HH:mm");
	private SimpleDateFormat m_YearFormat = new SimpleDateFormat("yyyy/MM/dd");
	private SimpleDateFormat m_monthFormat = new SimpleDateFormat("MM/dd");
	private SimpleDateFormat m_5YearFormat = new SimpleDateFormat("yyyy/MM");
	private SimpleDateFormat m_DateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
	private SimpleDateFormat m_DCFormat = new SimpleDateFormat("HH:mm");

	protected Core m_lib;

	public Core getTalib()
	{
		return m_lib;
	}

	public void setTalib(Core lib)
	{
		this.m_lib = m_lib;
	}

	protected String getDisplayX(ChartData.ChartPeroid pChartType, CandleData pData)
	{

		Date tDate = null;
		boolean isNowYear = false;

		try
		{

			long date = pData.m_nDate;
			// AN-1011, Lily said, "app分時線顯示自然日，日週月線顯示交易日"
//			if (pChartType == ChartPeroid.CP_1min
//					|| pChartType == ChartPeroid.CP_5min || pChartType == ChartPeroid.CP_15min
//					|| pChartType == ChartPeroid.CP_1hour)
//			{
//				date = pData.m_nDate;
//			}
			tDate = TimeUtils.millis2Date(date);
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(tDate);

			Calendar cal = Calendar.getInstance();

			isNowYear = cal.get(Calendar.YEAR) == calendar.get(Calendar.YEAR) ? true : false;
		}
		catch (Exception e1)
		{
			e1.printStackTrace();
		}

//		if (pChartType == ChartPeroid.CP_1min || pChartType == ChartPeroid.CP_5min)
//		{
//			return String.format("%02d", CommonLib.getHour(pData.m_nTime)) + ":"
//					+ String.format("%02d", CommonLib.getMinute(pData.m_nTime));
//		}
		if (pChartType == ChartData.ChartPeroid.CP_1week || pChartType == ChartData.ChartPeroid.CP_1day)
		{
			return isNowYear ?  m_monthFormat.format(tDate) : m_YearFormat.format(tDate);
		}
		else if (pChartType == ChartData.ChartPeroid.CP_1month)
		{
			return isNowYear ?  m_monthFormat.format(tDate) : m_5YearFormat.format(tDate);
		}
		else if(pChartType == ChartData.ChartPeroid.CP_DC)
		{
			return m_DCFormat.format(tDate);
		}
		else
		{
			return m_Format.format(tDate);
		}
	}

	public void drawYesterClose(Canvas pCanvas, Paint pPaint)
	{
		if (m_bShowYesterClose)
		{
			Coordinate tCoordinate = m_callback.getCoordinate();
			boolean isCoordinateTextOuter = tCoordinate.isCoordinateTextOuterY();
			CoinChart.PointLocation tPointLocation = tCoordinate.getOriginPointLocation();

			float tYMaxValue = tCoordinate.getMaxValue();
			float tYMinValue = tCoordinate.getMinValue();
			float tYValueDist = tYMaxValue - tYMinValue;

//			pPaint.setColor(Color.parseColor("#CCCCCC"));


			float tYesterPosition = tCoordinate.getYFirstPosition() - ((m_fYesterClose - tYMinValue) * (tCoordinate.getValueHeight()) / tYValueDist);
			//float tYesterPosition = tCoordinate.getLayerRectF().bottom - ((m_fYesterClose - tYMinValue) * (tCoordinate.getLayerRectF().bottom - tCoordinate.getLayerRectF().top) / tYValueDist);
//			pCanvas.drawLine(tCoordinate.getLayerRectF().left, tYesterPosition, tCoordinate.getLayerRectF().right, tYesterPosition, pPaint);

			pPaint.reset();
			pPaint.setColor(0xFF999999);
			pPaint.setStyle(Style.STROKE);
			pPaint.setStrokeWidth(3);
			pPaint.setAntiAlias(true);
			PathEffect effects = new DashPathEffect(new float[]
					{10, 6, 10, 6}, 5);
			pPaint.setPathEffect(effects);

			Path tPath = new Path();
			tPath.moveTo(tCoordinate.getLayerRectF().left, tYesterPosition);
			tPath.lineTo(tCoordinate.getLayerRectF().right, tYesterPosition);
			pCanvas.drawPath(tPath, pPaint);

			//绘制箭头和文字
			pPaint.reset();
			pPaint.setColor(0xFF999999);
			pPaint.setStyle(Style.FILL_AND_STROKE);
			float tTextSize = tCoordinate.getYTextSize();
			pPaint.setTextSize(tTextSize);
			float tTextSpace = tCoordinate.getYTextSpace();//获取文字和空隙的总区域
			String tText = "";
			if(m_fYesterClose != Float.MIN_VALUE)
			{
				tText = FormatUtil.getFormatString(m_fYesterClose, tCoordinate.getDigit());//获取格式化后要显示的文字
			}
			float tTextWidth = pPaint.measureText(tText);//获取文字的宽度
			float tPadding = tTextSpace/6.f;//(tTextSpace - tTextWidth) * 3 / 8;//计算padding，空隙减文字宽度的3/8
			//tPadding = tPadding > 5.f ? tPadding : 5;//padding最小为1
//			float tYSpace = tTextSpace - tTextWidth - tPadding* 2;//计算空隙的大小
//			tYSpace = tYSpace < 5 ? 5 : tYSpace;
//			float tYSpace = tTextSpace/6.f;

			if (tPointLocation == CoinChart.PointLocation.BOTTOMLEFT || tPointLocation == CoinChart.PointLocation.TOPLEFT)
			{
				if (isCoordinateTextOuter)
				{
					Bitmap tBitmapArrow = m_callback.getArrowRight();

					float tArrowHeight = tTextSize + 10;
					tBitmapArrow = Bitmap.createScaledBitmap(tBitmapArrow, (int) tPadding, (int) tArrowHeight, false);

					float tHalfBitmapHieght = tArrowHeight/ 2;
					float tBitmapOffset = tHalfBitmapHieght / 4;

					pCanvas.drawBitmap(tBitmapArrow, tCoordinate.getLayerRectF().left - tBitmapArrow.getWidth(), tYesterPosition - tHalfBitmapHieght, pPaint);
					pCanvas.drawRoundRect(new RectF(tCoordinate.getLayerRectF().left - tPadding- tTextWidth - tBitmapOffset, tYesterPosition - tHalfBitmapHieght, tCoordinate.getLayerRectF().left - tPadding+ tBitmapOffset, tYesterPosition + tHalfBitmapHieght), tBitmapOffset, tBitmapOffset, pPaint);

					pPaint.setColor(Color.WHITE);
					pCanvas.drawText(tText, tCoordinate.getLayerRectF().left - tPadding- tTextWidth, tYesterPosition + tTextSize / 2, pPaint);
				}
				else
				{
//					Bitmap tBitmapArrow = m_callback.getArrowLeft();

					float tArrowHeight = tTextSize + 10;
//					tBitmapArrow = Bitmap.createScaledBitmap(tBitmapArrow, (int) tPadding, (int) tArrowHeight, false);

					float tHalfBitmapHieght = tArrowHeight/ 2;
					float tBitmapOffset = 0;
//					float tBitmapOffset = tHalfBitmapHieght / 4;

//					pCanvas.drawBitmap(tBitmapArrow, tCoordinate.getLayerRectF().left + tBitmapOffset, tYesterPosition - tHalfBitmapHieght, pPaint);
					pCanvas.drawRoundRect(new RectF(tCoordinate.getLayerRectF().left, tYesterPosition - tHalfBitmapHieght, tCoordinate.getLayerRectF().left + 2 * tPadding + tTextWidth, tYesterPosition + tHalfBitmapHieght), tBitmapOffset, tBitmapOffset, pPaint);

					pPaint.setColor(Color.WHITE);
					pCanvas.drawText(tText, tCoordinate.getLayerRectF().left + tPadding, tYesterPosition + tTextSize / 2, pPaint);
				}
			}
			else
			{
				if (isCoordinateTextOuter)
				{
					Bitmap tBitmapArrow = m_callback.getArrowLeft();

					float tArrowHeight = tTextSize + 10;
					tBitmapArrow = Bitmap.createScaledBitmap(tBitmapArrow, (int) tPadding, (int) tArrowHeight, false);

					float tHalfBitmapHieght = tBitmapArrow.getHeight() / 2;
					float tBitmapOffset = tHalfBitmapHieght / 4;

					pCanvas.drawBitmap(tBitmapArrow, tCoordinate.getLayerRectF().right, tYesterPosition - tHalfBitmapHieght, pPaint);
					pCanvas.drawRoundRect(new RectF(tCoordinate.getLayerRectF().right + tBitmapArrow.getWidth() - tBitmapOffset, tYesterPosition - tHalfBitmapHieght, tCoordinate.getLayerRectF().right + tBitmapArrow.getWidth() + tTextWidth, tYesterPosition + tHalfBitmapHieght), tBitmapOffset, tBitmapOffset, pPaint);

					pPaint.setColor(Color.WHITE);
					pCanvas.drawText(tText, tCoordinate.getLayerRectF().right + tBitmapArrow.getWidth(), tYesterPosition + tTextSize / 2, pPaint);
				}
				else
				{
					Bitmap tBitmapArrow = m_callback.getArrowRight();

					float tArrowHeight = tTextSize + 10;
					tBitmapArrow = Bitmap.createScaledBitmap(tBitmapArrow, (int) tPadding, (int) tArrowHeight, false);

					float tHalfBitmapHieght = tBitmapArrow.getHeight() / 2;
					float tBitmapOffset = 0;
//					float tBitmapOffset = tHalfBitmapHieght / 4;

					pCanvas.drawBitmap(tBitmapArrow, tCoordinate.getLayerRectF().right - tBitmapArrow.getWidth(), tYesterPosition - tHalfBitmapHieght, pPaint);
					pCanvas.drawRoundRect(new RectF(tCoordinate.getLayerRectF().right - tBitmapArrow.getWidth() - tTextWidth, tYesterPosition - tHalfBitmapHieght, tCoordinate.getLayerRectF().right - tBitmapArrow.getWidth(), tYesterPosition + tHalfBitmapHieght), tBitmapOffset, tBitmapOffset, pPaint);

					pPaint.setColor(Color.WHITE);
					pCanvas.drawText(tText, tCoordinate.getLayerRectF().right - tBitmapArrow.getWidth() - tTextWidth, tYesterPosition + tTextSize / 2, pPaint);
				}
			}
		}
	}

	/**
	 * 是否按压在layer内
	 * @return
	 */
	public boolean IsPointInLayer()
	{
		Point tPoint = m_point;
		Coordinate tCoordinate = m_callback.getCoordinate();
		RectF tLayerRect = tCoordinate.getLayerRectF();

		if(tPoint != null && tLayerRect.contains(tPoint.x, tPoint.y) && m_callback.isShowLayer())
		{
			return true;
		}
		return false;

	}

	public void drawCrossText(Canvas pCanvas, Paint pPaint)
	{
//		FDTLogUtils.d("tingtouch","drawCrossText");
		String tTimeText = m_strXText;
		String tValueText = m_strYText;
		Point tPoint = m_point;


		Coordinate tCoordinate = m_callback.getCoordinate();



		CoinChart.PointLocation tPointLocation = tCoordinate.getOriginPointLocation();
		RectF tLayerRect = tCoordinate.getLayerRectF();
		RectF tChartRect = tCoordinate.getChartRectF();

		Layer.ChartTypeEnum chartTypeEnum = getLayerTypeEnum();
		if (!m_callback.needCrossLine() || tPoint == null || !tLayerRect.contains(tPoint.x, tPoint.y) || !m_callback.isShowLayer())
		{
			return;
		}

         //画复权信息，时间+复权说明
		drawExrightText(pCanvas, pPaint, tCoordinate);


		int tCrossColor = Color.parseColor("#A8A8A8");
		pPaint.setColor(tCrossColor);
		pCanvas.drawCircle(tPoint.x, tPoint.y, 5, pPaint);
		pCanvas.drawLine(tChartRect.left, tPoint.y, tChartRect.right, tPoint.y, pPaint);

		pCanvas.drawLine(tPoint.x, tChartRect.top + tChartRect.height()/20, tPoint.x, tChartRect.bottom, pPaint);//+ tChartRect.height()/20是因为上面有文字

		pPaint.setTextSize(tCoordinate.getXTextSize());

		float tTimeTextWidth = pPaint.measureText(tTimeText);
		float tValueTextWidth = pPaint.measureText(tValueText);
		float tPadding = 10.f;
		float tRoundRadius = 0.f;
		pPaint.setStyle(Style.FILL);
		pPaint.setColor(CoinChart.getCrossRectColor());
		if (m_callback.isCrossOutter())
		{
			if (tPointLocation == CoinChart.PointLocation.TOPRIGHT || tPointLocation == CoinChart.PointLocation.BOTTOMRIGHT)
			{
				pCanvas.drawRoundRect(new RectF(tLayerRect.right + (tCoordinate.getYTextSpace() - tValueTextWidth) / 2 - tPadding, tPoint.y - tPadding - pPaint.getTextSize() / 2, tLayerRect.right + (tCoordinate.getYTextSpace() + tValueTextWidth) / 2 + tPadding, tPoint.y + tPadding + pPaint.getTextSize() / 2), tRoundRadius, tRoundRadius, pPaint);
				pPaint.setColor(CoinChart.getCrossTextColor());
				pCanvas.drawText(tValueText, tLayerRect.right + (tCoordinate.getYTextSpace() - tValueTextWidth) / 2, tPoint.y + pPaint.getTextSize() / 2, pPaint);
			}
			else
			{
				pCanvas.drawRoundRect(new RectF(tLayerRect.left - (tValueTextWidth + tCoordinate.getYTextSpace()) / 2 - tPadding, tPoint.y - tPadding - pPaint.getTextSize() / 2, tLayerRect.left + (tValueTextWidth - tCoordinate.getYTextSpace()) / 2 + tPadding, tPoint.y + tPadding + pPaint.getTextSize() / 2), tRoundRadius, tRoundRadius, pPaint);
				pPaint.setColor(CoinChart.getCrossTextColor());
				pCanvas.drawText(tValueText, tLayerRect.left - (tCoordinate.getYTextSpace() + tValueTextWidth) / 2, tPoint.y + pPaint.getTextSize() / 2, pPaint);

			}

			pPaint.setColor(CoinChart.getCrossRectColor());

			if (tPointLocation == CoinChart.PointLocation.BOTTOMLEFT || tPointLocation == CoinChart.PointLocation.BOTTOMRIGHT)
			{

				float xLocation = tPoint.x - tTimeTextWidth / 2 - tPadding;
				if(tChartRect.left > xLocation)
				{
					xLocation = tChartRect.left;
				}
				else if((tPoint.x + tTimeTextWidth + tPadding) > tChartRect.right)
				{
					xLocation = tChartRect.right - tTimeTextWidth - tPadding;
				}

				pCanvas.drawRoundRect(new RectF(xLocation, tChartRect.bottom + (tCoordinate.getXTextSpace() - pPaint.getTextSize()) / 2 - tPadding, xLocation + tTimeTextWidth + 2 * tPadding, tChartRect.bottom + (tCoordinate.getXTextSpace() + pPaint.getTextSize()) / 2 + tPadding), tRoundRadius, tRoundRadius, pPaint);
				pPaint.setColor(CoinChart.getCrossTextColor());
				pCanvas.drawText(tTimeText, xLocation + tPadding, tChartRect.bottom + (tCoordinate.getXTextSpace() + pPaint.getTextSize()) / 2, pPaint);

//				pCanvas.drawRoundRect(new RectF(tPoint.x - tTimeTextWidth / 2 - tPadding, tChartRect.bottom + (tCoordinate.getXTextSpace() - pPaint.getTextSize()) / 2 - tPadding, tPoint.x + tTimeTextWidth / 2 + tPadding, tChartRect.bottom + (tCoordinate.getXTextSpace() + pPaint.getTextSize()) / 2 + tPadding), tRoundRadius, tRoundRadius, pPaint);
//				pPaint.setColor(Color.WHITE);
//				pCanvas.drawText(tTimeText, tPoint.x - tTimeTextWidth / 2, tChartRect.bottom + (tCoordinate.getXTextSpace() + pPaint.getTextSize()) / 2, pPaint);
			}
			else
			{
				pCanvas.drawRoundRect(new RectF(tPoint.x - tTimeTextWidth / 2 - tPadding, tChartRect.top - (tCoordinate.getXTextSpace() + pPaint.getTextSize()) / 2 - tPadding, tPoint.x + tTimeTextWidth / 2 + tPadding, tChartRect.top - (tCoordinate.getXTextSpace() - pPaint.getTextSize()) / 2 + tPadding), tRoundRadius, tRoundRadius, pPaint);
				pPaint.setColor(CoinChart.getCrossTextColor());
				pCanvas.drawText(tTimeText, tPoint.x - tTimeTextWidth / 2, tChartRect.top - (tCoordinate.getXTextSpace() - pPaint.getTextSize()) / 2, pPaint);
			}
		}
		else
		{
			if (tPointLocation == CoinChart.PointLocation.TOPRIGHT || tPointLocation == CoinChart.PointLocation.BOTTOMRIGHT)
			{
				pCanvas.drawRoundRect(new RectF(tLayerRect.right - tValueTextWidth - tPadding * 2, tPoint.y - tPadding - pPaint.getTextSize() / 2, tLayerRect.right, tPoint.y + tPadding + pPaint.getTextSize() / 2), tRoundRadius, tRoundRadius, pPaint);
				pPaint.setColor(CoinChart.getCrossTextColor());
				pCanvas.drawText(tValueText, tLayerRect.right - tValueTextWidth - tPadding, tPoint.y + pPaint.getTextSize() / 2, pPaint);
			}
			else
			{
				pCanvas.drawRoundRect(new RectF(tLayerRect.left, tPoint.y - tPadding - pPaint.getTextSize() / 2, tLayerRect.left + tValueTextWidth + tPadding * 2, tPoint.y + tPadding + pPaint.getTextSize() / 2), tRoundRadius, tRoundRadius, pPaint);
				pPaint.setColor(CoinChart.getCrossTextColor());
				pCanvas.drawText(tValueText, tLayerRect.left + tPadding, tPoint.y + pPaint.getTextSize() / 2, pPaint);
			}

			pPaint.setColor(CoinChart.getCrossRectColor());

			if (tPointLocation == CoinChart.PointLocation.BOTTOMLEFT || tPointLocation == CoinChart.PointLocation.BOTTOMRIGHT)
			{

				//如果x坐标超出了图的范围，那么把它移到边上
				float xLocation = tPoint.x - tTimeTextWidth / 2 - tPadding;
				if(tChartRect.left > xLocation)
				{
					xLocation = tChartRect.left;
				}
				else if((tPoint.x + tTimeTextWidth + tPadding) > tChartRect.right)
				{
					xLocation = tChartRect.right - tTimeTextWidth - tPadding;
				}
				if(CoinChart.is8btcStyle)
				{
					float yLocation = tChartRect.top + (tChartRect.bottom - tChartRect.top) * RouterHelper.<IDetailKlineBaseFragmentProvider>getProvider(RouterPath.Detail_KLINE_BASE_FRAGMENT_PROVIDER).getUpLayerHeight();
					pCanvas.drawRoundRect(new RectF(xLocation, yLocation + (tCoordinate.getXTextSpace() - pPaint.getTextSize()) / 2 - tPadding, xLocation + tTimeTextWidth + 2 * tPadding, yLocation  + (tCoordinate.getXTextSpace() + pPaint.getTextSize()) / 2 + tPadding), tRoundRadius, tRoundRadius, pPaint);
					pPaint.setColor(CoinChart.getCrossTextColor());
					pCanvas.drawText(tTimeText, xLocation + tPadding, yLocation + (tCoordinate.getXTextSpace() + pPaint.getTextSize()) / 2, pPaint);
				}
				else
				{
					pCanvas.drawRoundRect(new RectF(xLocation, tChartRect.bottom + (tCoordinate.getXTextSpace() - pPaint.getTextSize()) / 2 - tPadding, xLocation + tTimeTextWidth + 2 * tPadding, tChartRect.bottom + (tCoordinate.getXTextSpace() + pPaint.getTextSize()) / 2 + tPadding), tRoundRadius, tRoundRadius, pPaint);
					pPaint.setColor(CoinChart.getCrossTextColor());
					pCanvas.drawText(tTimeText, xLocation + tPadding, tChartRect.bottom + (tCoordinate.getXTextSpace() + pPaint.getTextSize()) / 2, pPaint);
				}

			}
			else
			{
				pCanvas.drawRoundRect(new RectF(tPoint.x - tTimeTextWidth / 2 - tPadding, tChartRect.top, tPoint.x + tTimeTextWidth / 2 + tPadding, tChartRect.top + pPaint.getTextSize() + tPadding * 2), tRoundRadius, tRoundRadius, pPaint);
				pPaint.setColor(CoinChart.getCrossTextColor());
				pCanvas.drawText(tTimeText, tPoint.x - tTimeTextWidth / 2, tChartRect.top + pPaint.getTextSize() + tPadding, pPaint);
			}
		}
	}

	/**
	 * 十字线的x轴移到除权日显示除权信息，如（2017-0605 （每股派息4毛钱））
	 * @param pCanvas
	 * @param pPaint
	 * @param tCoordinate
	 */
	private void drawExrightText(Canvas pCanvas, Paint pPaint, Coordinate tCoordinate) {
		if(mCandleData==null) return;
	}

	/**
	 * convert exright to display Format
	 * @param factor
	 * @return
	 */
	private String formatExrightFactor(ChartData.ExrightFactor factor) {
		if(factor==null||factor.getDate()==0||factor.getDescription()==null) return "null";

		SimpleDateFormat originFormat = new SimpleDateFormat("yyyyMMdd");
		SimpleDateFormat targetFormat = new SimpleDateFormat("yyyy-MM-dd");
		try {
			Date date=originFormat.parse(String.valueOf(factor.getDate()));
			return targetFormat.format(date)+" ("+factor.getDescription()+")";
		} catch (Exception e) {
			e.printStackTrace();
		}

		return "null";
	}

	/**
	 * 绘制浮动框，显示当前layer信息框
	 * @param pCanvas
	 * @param pPaint
	 */
	public void drawFloatPanel(Canvas pCanvas, Paint pPaint)
	{
		if (!m_callback.needFloatPanel())
			return;
		String tTimeText = m_strXText;//获得点击的点的线上点的值和日期
		String tNumText = m_strYText;//获得当前点对应的线上的点的盈亏
		try
		{
			DecimalFormat df = new DecimalFormat(",##0.00");
			tNumText = df.format(Double.parseDouble(tNumText));
		}catch (Exception e)
		{

		}

		String tValueText = "";
		int upColor = 0xFFE14440;
		int downColor = 0xFF1EAC3E;
		int myColor;
		if(tNumText.startsWith("-"))
		{
			myColor = downColor;
		}else
		{
			myColor = upColor;
		}

		Point tPoint = m_point;

		String valueTitle = "";//MyApplication.getIns().getResources().getString("trader_grand_realizepltitle");
		tValueText = valueTitle + tNumText;



		Coordinate tCoordinate = m_callback.getCoordinate();
		CoinChart.PointLocation tPointLocation = tCoordinate.getOriginPointLocation();
		RectF tLayerRect = tCoordinate.getChartRectF();



//		if (tPoint == null || !tLayerRect.contains(tPoint.x, tPoint.y))
//		{
//			if(tPoint.x == tLayerRect.right)//刚好靠右
//			{
//
//			}else
//			{
//				return;
//			}
//		}


		pPaint.setTextSize(DimentionsUtil.dp2px(12));
		float tValueTextWidth = pPaint.measureText(tValueText);
		float tValueTitleWidth = pPaint.measureText(valueTitle);
		float tPadding = DimentionsUtil.dp2px(7);
		float tYPadding = DimentionsUtil.dp2px(16);
		float tRoundRadius = 10.f;


		//在手指按压的时候，弹窗浮动在手指热区的周围，距离手指的热区24dp／pt；
		//如果手指按住的位置向上不足框的高度，就显示在手指下方，如果按住的位置向右不足框的宽度，就显示在手指左边
		pPaint.setStyle(Style.STROKE);//空心矩形框
		boolean drawLeft = false;
		boolean drawDown = false;
		float distFromTouch = 40;
		if(tPoint.x + tValueTextWidth + 2 * tPadding > tLayerRect.right)
		{
			drawLeft = true;
		}
		if(tPoint.y - distFromTouch  - 2 * pPaint.getTextSize() - 2 * tPadding < tLayerRect.top)
		{
			drawDown = true;
		}
		if (drawLeft && drawDown)
		{

			pPaint.setColor(0xFF407ADA);//蓝色外边框圆
			pCanvas.drawRoundRect(new RectF(tPoint.x - (tValueTextWidth + 2 * tPadding), tPoint.y + distFromTouch, tPoint.x, tPoint.y + distFromTouch + (2 * pPaint.getTextSize() + 2 * tYPadding)), tRoundRadius, tRoundRadius, pPaint);
//			pCanvas.drawLine(tPoint.x,);//画柱子
			pPaint.setStyle(Style.FILL);
			pPaint.setColor(0xFFFFFFFF);
			pCanvas.drawRoundRect(new RectF(tPoint.x - (tValueTextWidth + 2 * tPadding), tPoint.y + distFromTouch, tPoint.x, tPoint.y + distFromTouch + (2 * pPaint.getTextSize() + 2 * tYPadding)), tRoundRadius, tRoundRadius, pPaint);
			pPaint.setColor(0xFF000000);//黑色字体
			pCanvas.drawText(tTimeText, tPoint.x - (tValueTextWidth + tPadding), tPoint.y + distFromTouch + (pPaint.getTextSize() + tYPadding), pPaint);
			pCanvas.drawText(valueTitle, tPoint.x - (tValueTextWidth + tPadding), tPoint.y + distFromTouch + (pPaint.getTextSize()*2 + tYPadding), pPaint);
			pPaint.setColor(myColor);//涨跌颜色
			pCanvas.drawText(tNumText, tPoint.x - (tValueTextWidth + tPadding) + tValueTitleWidth, tPoint.y + distFromTouch + (pPaint.getTextSize()*2 + tYPadding), pPaint);
		}
		else if (drawLeft)
		{
			pPaint.setColor(0xFF407ADA);//蓝色外边框圆
			pCanvas.drawRoundRect(new RectF(tPoint.x - (tValueTextWidth + 2 * tPadding), tPoint.y - distFromTouch - (2 * pPaint.getTextSize() + 2 * tYPadding), tPoint.x, tPoint.y - distFromTouch), tRoundRadius, tRoundRadius, pPaint);
			pPaint.setStyle(Style.FILL);
			pPaint.setColor(0xFFFFFFFF);
			pCanvas.drawRoundRect(new RectF(tPoint.x - (tValueTextWidth + 2 * tPadding), tPoint.y - distFromTouch - (2 * pPaint.getTextSize() + 2 * tYPadding), tPoint.x, tPoint.y - distFromTouch), tRoundRadius, tRoundRadius, pPaint);
			pPaint.setColor(0xFF000000);//黑色字体
			pCanvas.drawText(tTimeText, tPoint.x - (tValueTextWidth + tPadding), tPoint.y - distFromTouch - (pPaint.getTextSize() + tYPadding), pPaint);
			pCanvas.drawText(valueTitle, tPoint.x - (tValueTextWidth + tPadding), tPoint.y - distFromTouch  - (tYPadding), pPaint);
			pPaint.setColor(myColor);//涨跌颜色
			pCanvas.drawText(tNumText, tPoint.x - (tValueTextWidth + tPadding) + tValueTitleWidth, tPoint.y - distFromTouch - (tYPadding), pPaint);
		}
		else if (drawDown)
		{
			pPaint.setColor(0xFF407ADA);//蓝色外边框圆
			pCanvas.drawRoundRect(new RectF(tPoint.x, tPoint.y + distFromTouch, tPoint.x + (tValueTextWidth + 2 * tPadding), tPoint.y + distFromTouch + (2 * pPaint.getTextSize() + 2 * tYPadding)), tRoundRadius, tRoundRadius, pPaint);
			pPaint.setStyle(Style.FILL);
			pPaint.setColor(0xFFFFFFFF);
			pCanvas.drawRoundRect(new RectF(tPoint.x, tPoint.y + distFromTouch, tPoint.x + (tValueTextWidth + 2 * tPadding), tPoint.y + distFromTouch + (2 * pPaint.getTextSize() + 2 * tYPadding)), tRoundRadius, tRoundRadius, pPaint);
			pPaint.setColor(0xFF000000);//黑色字体
			pCanvas.drawText(tTimeText, tPoint.x + (tPadding), tPoint.y + distFromTouch + (pPaint.getTextSize() + tYPadding), pPaint);
			pCanvas.drawText(valueTitle, tPoint.x + (tPadding), tPoint.y + distFromTouch + (pPaint.getTextSize()*2 + tYPadding), pPaint);
			pPaint.setColor(myColor);//涨跌颜色
			pCanvas.drawText(tNumText, tPoint.x + (tPadding) + tValueTitleWidth, tPoint.y + distFromTouch + (pPaint.getTextSize()*2 + tYPadding), pPaint);
		}
		else
		{
			pPaint.setColor(0xFF407ADA);//蓝色外边框圆
			pCanvas.drawRoundRect(new RectF(tPoint.x, tPoint.y - distFromTouch - (2 * pPaint.getTextSize() + 2 * tYPadding), tPoint.x + (tValueTextWidth + 2 * tPadding), tPoint.y - distFromTouch), tRoundRadius, tRoundRadius, pPaint);
			pPaint.setStyle(Style.FILL);
			pPaint.setColor(0xFFFFFFFF);
			pCanvas.drawRoundRect(new RectF(tPoint.x, tPoint.y - distFromTouch - (2 * pPaint.getTextSize() + 2 * tYPadding), tPoint.x + (tValueTextWidth + 2 * tPadding), tPoint.y - distFromTouch), tRoundRadius, tRoundRadius, pPaint);
			pPaint.setColor(0xFF000000);//黑色字体
			pCanvas.drawText(tTimeText, tPoint.x + (tPadding), tPoint.y - distFromTouch - (pPaint.getTextSize() + tYPadding), pPaint);
			pCanvas.drawText(valueTitle, tPoint.x + (tPadding), tPoint.y - distFromTouch - (tYPadding), pPaint);
			pPaint.setColor(myColor);//涨跌颜色
			pCanvas.drawText(tNumText, tPoint.x + (tPadding) + tValueTitleWidth, tPoint.y - distFromTouch - (tYPadding), pPaint);
		}
		pPaint.setStrokeWidth(1);
		pPaint.setColor(0xFF407ADA);
		pCanvas.drawLine(tPoint.x,tLayerRect.bottom,tPoint.x,tLayerRect.top,pPaint);
	}

	protected void setCrossData(Canvas pCanvas, Paint pPaint, String pXText, String pYText, Point pPoint)
	{
		String tTimeText = pXText == null ? "" : pXText;
		String tValueText = pYText == null ? "" : pYText;

		m_strXText = tTimeText;
		m_strYText = tValueText;

		m_point = pPoint;
	}


	protected void setCrossData(Canvas pCanvas, Paint pPaint, String pXText, String pYText, Point pPoint,CandleData candleData)
	{
		String tTimeText = pXText == null ? "" : pXText;
		String tValueText = pYText == null ? "" : pYText;

		m_strXText = tTimeText;
		m_strYText = tValueText;

		m_point = pPoint;
		mCandleData=candleData;
	}


	protected void drawMaxMin(Canvas pCanvas, Paint pPaint, Coordinate tCoordinate, int digit,  float tXPosition,int index,CandleData candleData) {
		if(tCoordinate.getIsShowMaxMinArrow()){

			int dp7 = (int)DimentionsUtil.dp2px(7);
			//画箭头最大值
            if (index == tCoordinate.getKMaxDateIndex()) {
                float middleX = tCoordinate.getLayerRectF().left+tCoordinate.getLayerRectF().width()/2;
                drawLineArrow(pCanvas, pPaint, new Point((int) tXPosition, (int) (tCoordinate.getYFirstPosition() - tCoordinate.getValueHeight() +dp7)), FormatUtil.getFormatString(candleData.getDbHigh(), digit), tXPosition < middleX ? true : false);
            }

            //画箭头最小值
            if (index == tCoordinate.getKMinDateIndex() ) {
				float middleX = tCoordinate.getLayerRectF().left+tCoordinate.getLayerRectF().width()/2;
                drawLineArrow(pCanvas, pPaint, new Point((int) tXPosition, (int) (tCoordinate.getYFirstPosition() - dp7)), FormatUtil.getFormatString(candleData.getDbLow(), digit), tXPosition < middleX ? true : false);
            }
        }
	}

	protected void drawLineMaxMin(Canvas pCanvas, Paint pPaint, Coordinate tCoordinate, int digit,  float tXPosition,int index,CandleData candleData,float tHighYPosition, float tLowYPosition) {
		if(tCoordinate.getIsShowMaxMinArrow()){

			int dp7 = 0;//(int)DimentionsUtil.dp2px(7);
			//画箭头最大值
			if (index == tCoordinate.getKMaxDateIndex()) {
				float middleX = tCoordinate.getLayerRectF().left+tCoordinate.getLayerRectF().width()/2;
				drawLineMaxMin(pCanvas, pPaint, new Point((int) tXPosition, (int)tHighYPosition), FormatUtil.getFormatString(candleData.getDbHigh(), digit), tXPosition < middleX ? true : false);
			}

			//画箭头最小值
			if (index == tCoordinate.getKMinDateIndex() ) {
				float middleX = tCoordinate.getLayerRectF().left+tCoordinate.getLayerRectF().width()/2;
				drawLineMaxMin(pCanvas, pPaint, new Point((int) tXPosition, (int)tLowYPosition), FormatUtil.getFormatString(candleData.getDbLow(), digit), tXPosition < middleX ? true : false);
			}
		}
	}

	public void drawFloatIndicatorText(boolean longtouch, Canvas pCanvas, Paint pPaint)
	{

	}



	public String getXText()
	{
		return m_strXText;
	}

	public String getYText()
	{
		return m_strYText;
	}

	public void needGradient(boolean gradient)
	{
		m_bGradient = gradient;
	}

	public void setLayerTypeCallback(LayerTypeCallback pCallback)
	{
		m_callback = pCallback;
	}

	public void showYesterClose(boolean pShow)
	{
		m_bShowYesterClose = pShow;
	}

	public boolean isShowYesterClose()
	{
		return m_bShowYesterClose;
	}

	public void setYesterClose(float pValue)
	{
		m_fYesterClose = pValue;
	}

	public float getYesterClose()
	{
		return m_fYesterClose;
	}

	public Coordinate createCoordinate()
	{
		return new Coordinate();
	}

	public void drawLayer(Canvas pCanvas, Paint pPaint, Point pPoint)
	{
		onDrawLayer(pCanvas, pPaint, pPoint);

		drawYesterClose(pCanvas, pPaint);
	}

	public abstract Layer.ChartTypeEnum getLayerTypeEnum();

	public abstract void onDrawLayer(Canvas pCanvas, Paint pPaint, Point pPoint);

	public boolean onCalculateData()
	{
		return true;
	}

	/**
	 * 画持仓成本线
	 * @param pCanvas
	 * @param pPaint
	 * @param tCoordinate
	 * @param tYFirstposition
	 * @param tYMinValue
	 * @param tYMaxValue
	 * @param tYValueDist
	 * @param tYValueLength
	 */
	public void drawAvgLine(Canvas pCanvas, Paint pPaint, Coordinate tCoordinate, float tYFirstposition, float tYMinValue, float tYMaxValue,float tYValueDist, float tYValueLength) {

		if(!m_callback.isShowLayer()) return;

//		if(tCoordinate==null||tCoordinate.tSymBol==null||tCoordinate.tSymBol.getPosition()==null) return;

		float avgPrice=0;//(float)tCoordinate.tSymBol.getPosition().getAvePriceValue();

		if(avgPrice==0) return;

		float tAvgYPosition = tYFirstposition - ((avgPrice - tYMinValue) * tYValueLength / tYValueDist);//获取均线high的y位置

//		FDTLogUtils.d("averageLine", "avgPrice:"+avgPrice+",max:"+tYMaxValue+",min:"+tYMinValue+",avgYPosition:"+tAvgYPosition);
		if(avgPrice>=tYMinValue&&avgPrice<=tYMaxValue){

			//backup paint state
			int originColor = pPaint.getColor();
			Style style = pPaint.getStyle();
			PathEffect pathEffect = pPaint.getPathEffect();

			pPaint.setColor(Color.parseColor("#FFCE3F2C"));


			pPaint.setStyle(Style.STROKE);
			pPaint.setPathEffect(new DashPathEffect(new float[]{8, 10, 8, 10}, 0));

			Path path = new Path();
			path.moveTo(tCoordinate.getLayerRectF().left, tAvgYPosition);
			path.lineTo(tCoordinate.getLayerRectF().right, tAvgYPosition);

			pCanvas.drawPath(path, pPaint);

            //recover paint state
			pPaint.setStyle(style);
			pPaint.setPathEffect(pathEffect);
			pPaint.setColor(originColor);

		}

	}

	public boolean isDrawAvgText(){
		return false;
	}

	/**
	 * 十字线显示时显示成本线的数值
	 * @param pCanvas
	 * @param pPaint
	 */
	public void drawAvgText(Canvas pCanvas, Paint pPaint) {

		if(!m_callback.isShowLayer()) return;

		if(!isDrawAvgText()){
			return;
		}


		Coordinate tCoordinate = m_callback.getCoordinate();

//		if(tCoordinate.tSymBol==null||tCoordinate.tSymBol.getPosition()==null||!tCoordinate.tSymBol.getPosition().hasPosition()) return;

		float tYMaxValue = tCoordinate.getMaxValue();
		float tYMinValue = tCoordinate.getMinValue();

		float tYFirstposition = tCoordinate.getYFirstPosition();
		float tYValueDist = tYMaxValue - tYMinValue;
		float tYValueLength = tCoordinate.getYInterval() * tCoordinate.getYIntervalCount();


		float avgPrice=0;//(float)tCoordinate.tSymBol.getPosition().getAvePriceValue();

		String avgPriceText=FormatUtil.getFormatString(avgPrice, m_callback.getDigit());


		float tAvgYPosition = tYFirstposition - ((avgPrice - tYMinValue) * tYValueLength / tYValueDist);//获取均线high的y位置

//		FDTLogUtils.d("averagetext", "avgPrice:"+avgPrice+",max:"+tYMaxValue+",min:"+tYMinValue+",avgYPosition:"+tAvgYPosition+"，firstPosition"+tYFirstposition);
//		if(avgPrice!=0&&tAvgYPosition<1||tYMaxValue>100000||tYMinValue>100000) return;


		if(avgPrice>=tYMinValue&&avgPrice<=tYMaxValue){


			pPaint.setColor(Color.parseColor("#FFCE3F2C"));

			float padding=10;

			float tTextSize = tCoordinate.getYTextSize();
			float textWidth = pPaint.measureText(avgPriceText);

			float x=tCoordinate.getLayerRectF().left+padding;//- textWidth-padding;
			float y=tAvgYPosition;
			float left=tCoordinate.getLayerRectF().left;
			float top=y-tTextSize/2-padding;
			float right=x+textWidth+padding;
			float bottom=y+tTextSize/2+padding;


			pCanvas.drawRoundRect(new RectF(left,top,right,bottom),0,0, pPaint);

			pPaint.setColor(Color.parseColor("#FFFFFFFF"));

			pCanvas.drawText(avgPriceText,x, y+(int)(tTextSize/2.5),pPaint);


		}

	}


	public void drawLineArrow(Canvas canvas,Paint paint, Point dest,String price,boolean isTextLeft){

//		FDTLogUtils.d("chen","drawlineArrow,point:"+dest.toString()+",price:"+price+",isleft:"+isTextLeft);
		paint.setTextSize(DimentionsUtil.sp2px(10));
		int arrowLength=(int)DimentionsUtil.dp2px(5),longLineLength=(int)DimentionsUtil.dp2px(20);
		int lineX;
		int arrowX;
		float textWidth=paint.measureText(price);
		float textX;
		if(isTextLeft){
			lineX=dest.x+longLineLength;
			arrowX=dest.x+arrowLength;
			textX=dest.x+longLineLength+(int)DimentionsUtil.dp2px(2);
		}else{
			lineX=dest.x-longLineLength;
			arrowX=dest.x-arrowLength;
			textX=dest.x-longLineLength-textWidth-(int)DimentionsUtil.dp2px(2);
		}

		Point longLine=new Point(lineX,dest.y);

		Point arrowUpLine=new Point(arrowX,dest.y-arrowLength/2);
		Point arrowDownLine=new Point(arrowX,dest.y+arrowLength/2);

		paint.setColor(Color.parseColor("#666666"));
		paint.setStrokeWidth(2);

        canvas.drawLine(dest.x, dest.y, longLine.x, longLine.y, paint);

		paint.setStyle(Style.FILL);
		Path arrowPath=new Path();
		arrowPath.moveTo(dest.x,dest.y);
		arrowPath.lineTo(arrowUpLine.x,arrowUpLine.y);
		arrowPath.lineTo(arrowDownLine.x,arrowDownLine.y);
		arrowPath.close();
		canvas.drawPath(arrowPath, paint);

//		canvas.drawLine(arrowUpLine.x,arrowUpLine.y,longLine.x,longLine.y,paint);
//
//		canvas.drawLine(arrowDownLine.x,arrowDownLine.y,longLine.x,longLine.y,paint);

		paint.reset();
		paint.setColor(Color.parseColor("#666666"));
		paint.setTextSize(DimentionsUtil.sp2px(10));
		canvas.drawText(price,textX,dest.y+paint.getTextSize()/3,paint);
	}

	/**
	 * 原为画箭头，现改为线
	 * @param canvas
	 * @param paint
	 * @param dest
	 * @param price
	 * @param isTextLeft
	 */
	public void drawLineMaxMin(Canvas canvas,Paint paint, Point dest,String price,boolean isTextLeft){

//		FDTLogUtils.d("chen","drawlineArrow,point:"+dest.toString()+",price:"+price+",isleft:"+isTextLeft);
		paint.setTextSize(DimentionsUtil.sp2px(10));
		int arrowLength=(int)DimentionsUtil.dp2px(0),longLineLength=(int)DimentionsUtil.dp2px(20);
		int lineX;
		int arrowX;
		float textWidth=paint.measureText(price);
		float textX;
		if(isTextLeft){
			lineX=dest.x+longLineLength;
			arrowX=dest.x+arrowLength;
			textX=dest.x+longLineLength;
		}else{
			lineX=dest.x-longLineLength;
			arrowX=dest.x-arrowLength;
			textX=dest.x-longLineLength-textWidth;
		}

		Point longLine=new Point(lineX,dest.y);

//		Point arrowUpLine=new Point(arrowX,dest.y-arrowLength/2);
//		Point arrowDownLine=new Point(arrowX,dest.y+arrowLength/2);

		paint.setColor(Color.parseColor("#4D566B"));
		paint.setStrokeWidth(2);

		canvas.drawLine(dest.x, dest.y, longLine.x, longLine.y, paint);

//		paint.setStyle(Style.FILL);
//		Path arrowPath=new Path();
//		arrowPath.moveTo(dest.x,dest.y);
//		arrowPath.lineTo(arrowUpLine.x,arrowUpLine.y);
//		arrowPath.lineTo(arrowDownLine.x,arrowDownLine.y);
//		arrowPath.close();
//		canvas.drawPath(arrowPath, paint);

//		canvas.drawLine(arrowUpLine.x,arrowUpLine.y,longLine.x,longLine.y,paint);
//
//		canvas.drawLine(arrowDownLine.x,arrowDownLine.y,longLine.x,longLine.y,paint);


		paint.reset();
		float tPadding = 8.f;
		paint.setTextSize(DimentionsUtil.sp2px(10));
		float tTextWidth = paint.measureText(price);//获取文字的宽度
		paint.setColor(Color.parseColor("#4D566B"));
		paint.setStyle(Style.FILL);
		canvas.drawRoundRect(new RectF(textX, dest.y + paint.getTextSize()/2 - paint.getTextSize(), textX + tTextWidth + tPadding * 2, dest.y+paint.getTextSize()/1.5f), 0, 0, paint);
		paint.setColor(Color.parseColor("#999999"));
		canvas.drawText(price,textX + tPadding,dest.y+paint.getTextSize()/2,paint);

	}






	public interface LayerTypeCallback
	{
		Coordinate getCoordinate();

		void connectChartDataFinished();

		int getSysUpColor();

		int getSysDownColor();

		int getSysBgColor();

		int getSysEvenColor();

		List<CandleData> getCandleDataList();

		ChartData.ChartPeroid getChartType();

		int getDigit();

		void pointFeedback(HashMap<String, String> pData, Point pPoint);

		boolean needCrossLine();

		boolean needFloatPanel();

		boolean needFloatIndicatorText();

		boolean isCrossOutter();

		int getColor();

		int getMACount();

		Bitmap getArrowLeft();

		Bitmap getArrowRight();

		int getDisplayCount();

		boolean isShowLayer();

		boolean isDrawAllData();

		void setAdditionData(List<CandleData> pAddition);

		boolean isShowShadow();
	}
}
